﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class Users_EditUsers : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            mvEditUsers.SetActiveView(vUsersList);
             
        }
    }
    protected void ibtnClose_Click(object sender, EventArgs e)
    {
        mvEditUsers.SetActiveView(vUsersList);
        gvUsers.DataBind();
    }

    protected void btnDelete_Command(object sender, CommandEventArgs e)
    {
        if (e.CommandName == "DeleteUser")
        {
            string userName = e.CommandArgument.ToString();
            if (!string.IsNullOrEmpty(userName))
            {
                Membership.DeleteUser(userName, true);
                mvEditUsers.SetActiveView(vUsersList);
                gvUsers.DataBind();
            }
        }
    }

    protected void ibtnEditUser_Command(object sender, CommandEventArgs e)
    {
        if (e.CommandArgument != null)
        {
            mvEditUsers.SetActiveView(vUserInfo);
            dvUserInfo.ChangeMode(DetailsViewMode.ReadOnly);
            hfUserName.Value = e.CommandArgument.ToString();
            ProfileCommon userProfile = Profile.GetProfile(e.CommandArgument.ToString());
            if (userProfile != null)
            {
                if (string.IsNullOrEmpty(userProfile.Sex))
                {
                    userProfile.Sex = "Male";
                    userProfile.Save();
                }
            }
        }
    }

    protected string GetEmailForUser(string userName)
    {
        MembershipUser user = Membership.GetUser(userName);
        string email = string.Empty;
        if (user != null)
        {
            email = user.Email;
        }
        return email;
    }
    
    protected void dvUserInfo_ItemUpdating(object sender, DetailsViewUpdateEventArgs e)
    {
        e.Cancel = true; // used to prevent ods update
        dvUserInfo.ChangeMode(DetailsViewMode.ReadOnly);
        string userName = e.Keys["UserName"].ToString();
        ProfileCommon userProfile = Profile.GetProfile(userName);
        MembershipUser user = Membership.GetUser(userName);
        if (user != null && userProfile != null)
        {
            if (e.NewValues["Email"] != null)
            {
                user.Email = e.NewValues["Email"].ToString();
            }
            if (e.NewValues["FirstName"] != null)
            {
                userProfile.FirstName = e.NewValues["FirstName"].ToString();
            }
            if (e.NewValues["MiddleName"] != null)
            {
                userProfile.MiddleName = e.NewValues["MiddleName"].ToString();
            }
            if (e.NewValues["LastName"] != null)
            {
                userProfile.LastName = e.NewValues["LastName"].ToString();
            }
            if (e.NewValues["Address"] != null)
            {
                userProfile.Address = e.NewValues["Address"].ToString();
            }
            if (e.NewValues["MobilePhone"] != null)
            {
                userProfile.MobilePhone = e.NewValues["MobilePhone"].ToString();
            }
            if (e.NewValues["Phone"] != null)
            {
                userProfile.Phone = e.NewValues["Phone"].ToString();
            }
            if (e.NewValues["Sex"] != null)
            {
                userProfile.Sex = e.NewValues["Sex"].ToString();
            }
            if (e.NewValues["Age"] != null)
            {
                int age;
                if (int.TryParse(e.NewValues["Age"].ToString(), out age))
                {
                    userProfile.Age = age.ToString();
                }
            }

            Membership.UpdateUser(user);
            userProfile.Save();
        }
        
    }
}
